<template>
	<view class="searchPage">
		<headerinfo></headerinfo>
		<u-navbar title="我的生活" :placeholder="true" leftIcon=" " :bgColor="bgColor" :titleStyle="titleStyle"></u-navbar>
		<view class="search">
			<u-icon name="arrow-left" size="20px" class="leftIcon" color="#fff" @click="back"></u-icon>
			<u-search
				height="37"
				bgColor="#fff"
				:actionStyle="actionStyle"
				shape="round"
				placeholder="请输入搜索关键字"
				:showAction="true"
				@search="searchSubmit"
				@custom="searchSubmit"
				v-model="query.searchContent"
			></u-search>
		</view>

		<view class="his" v-if="hislist.length > 0">
			<view class="title">
				<span>历史搜索</span>
				<i class="iconfont" @click="clearHis">&#xe74b;</i>
			</view>
			<view class="list">
				<span @click="copyValue(item)" v-for="(item, index) in hislist" :key="index">{{ item }}</span>
			</view>
		</view>
		<view class="moreSearchBox" v-show="showMoreSearch">
			<!-- 分类 -->
			<view class="his" v-if="tabTitle.length > 0">
				<view class="title"><span>分类</span><span @click="clearType" style="font-size: 28rpx;color: #929292;">清空</span></view>
				<view class="list">
					<span :class="item.checked ? 'active' : ''" @click="checkFenlei(index)" v-for="(item, index) in tabTitle" :key="index">{{ item.name }}</span>
				</view>
			</view>
			<view class="his">
				<view class="title">
					<span>日期范围</span>
					<span @click="clearDate" style="font-size: 28rpx;color: #929292;">清空</span>
				</view>
				<view class="list" @click="rangeVisible = true">
					<span class="dateSpan" style="margin-right: 0;">{{startDate?startDate:'开始日期'}}</span>
					<div class="zhi">至</div>
					<span class="dateSpan" >{{endDate?endDate:'结束日期'}}</span>
				</view>
			</view>
			
		</view>

		<view class="moreSearch" @click="showMoreSearch = !showMoreSearch">
			更多筛选条件
			<i class="iconfont" :class="showMoreSearch ? 'upIcon' : ''">&#xe606;</i>
		</view>
		
		

		<w-picker :visible.sync="rangeVisible" mode="range" startYear="2020" endYear="2099" :current="true" @confirm="onConfirm($event)" ref="range"></w-picker>
	</view>
</template>

<script>
export default {
	data() {
		return {
			bgColor: '#de4849',
			titleStyle: {
				color: '#fff'
			},
			startDate:'',   //正常日期
			endDate:'',
			query: {
				searchContent: '',
				categoryIds: [],
				endDate: '',    //时间戳格式
				startDate: '',
				pageSize:10,
				lastDocId:'',
				isDraft:false,
				username: ''
			},
			tabTitle: [],
			hislist: uni.getStorageSync('lifehislist') ? uni.getStorageSync('lifehislist') : [],
			actionStyle: {
				color: '#fff'
			},
			showMoreSearch: false,
			rangeVisible: false
		};
	},
	onLoad(e) {
		console.log('mylifesearch onLoad e',e);
		if(e.username){
			this.query.username = e.username
		}
		this.getAllCategories();
	},
	methods: {
		clearDate(){
			this.query.startDate = this.startDate = this.query.endDate = this.endDate = ''
		},
		clearType(){
			this.tabTitle.forEach(item=>{
				item.checked = false
			})
		},
	
		onConfirm(res) {
			this.startDate = this.trimSpecial(res.value[0])
			this.endDate = this.trimSpecial(res.value[1])
			console.log(this.startDate)
			console.log(this.endDate)
			console.log(res)
		},
		trimSpecial(string) {
		
		  //替换字符串中的所有特殊字符（包含空格）
		  if(string!= ""){
		    const pattern=/[`~!@#$^\-&*()=|{}':;',\\\[\]\.<>\/?~！@#￥……&*（）——|{}【】'；：""'。，、？\s]/g;
		    string = string.replace(pattern,"");
		  }
		  return string
		},
		back() {
			uni.navigateBack();
		},
		clearHis() {
			this.hislist = [];
			uni.setStorageSync('lifehislist', []);
		},
		copyValue(data) {
			this.query.searchContent = data;
			this.goSearch();
		},
		setHistory() {
			// 只有查自己的生活才需要记录
			if (this.query.searchContent && (!this.query.username)) {
				// this.hislist.push(this.query.searchContent);
				// uni.setStorageSync('lifehislist', this.hislist);
				// 加入搜索历史
				let index = this.hislist.findIndex(v => v===this.query.searchContent)
				if(index !== -1){
					this.hislist.unshift(this.hislist.splice(index,1)[0])
				} else {
					this.hislist.unshift(this.query.searchContent)
				}
				uni.setStorageSync('lifehislist', this.hislist);
			}
		},
		searchSubmit() {
		
			this.setHistory();
			this.goSearch();
		},
		goSearch() {
			console.log(this.query)
			
			let arr = []
			if(this.tabTitle.length>0){
				this.tabTitle.forEach(item=>{
					if(item.checked){
						arr.push(item.id)
					}
				})
			}
			
			this.query.categoryIds = arr
			
			
			if(this.startDate){
				this.query.startDate = this.startDate
				this.query.endDate = this.endDate
			}
			
			if(this.query.searchContent == ''){
				delete this.query.searchContent
			}
			
			let queryData = JSON.stringify(this.query)
			console.log(queryData)
			
			uni.navigateTo({
				url:`/pages/news/searchover?queryData=${queryData}&isMylife=1`
			})
		},
		getAllCategories() {
			let tabBars = [];
			this.$api.getAllCategories().then(res => {
				if (res) {
					for (let i in res) {
						tabBars.push({
							name: res[i],
							id: i,
							checked: false
						});
					}
					this.tabTitle = tabBars;
				}
			});
		},
		checkFenlei(index) {
			this.tabTitle[index].checked = !this.tabTitle[index].checked;
		},
		
		
	}
};
</script>

<style>
page {
	background-color: #fff;
}
</style>
<style lang="scss" scoped>
.barHeight {
	height: var(--status-bar-height);
	background-color: #de4849;
}

.moreSearch {
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 26rpx;
	color: #7e7c7c;
	margin-top: 40rpx;
	display: flex;
	align-items: center;
	.iconfont {
		margin-left: 10rpx;
		font-size: 30rpx;
		display: block;
	}
	.upIcon {
		transform: rotate(180deg);
	}
}

.search {
	background-color: #de4849;
	display: flex;
	align-items: center;
	justify-content: space-between;
	height: 120rpx;
	padding: 0 30rpx;
	width: 100%;
	z-index: 100;
	.leftIcon {
		margin-right: 20rpx;
	}
}
.his {
	margin-top: 30rpx;
	padding: 0 20rpx;
	.title {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 20rpx;
		span {
			font-size: 30rpx;
		}
	}
	.list {
		display: flex;
		align-items: center;
		flex-wrap: wrap;
		span {
			padding: 4px 20px;
			border-radius: 5px;
			background-color: #fff;
			border: 1px solid #b7b7b7;
			font-size: 26rpx;
			margin-right: 20rpx;
			margin-bottom: 20rpx;
			color: #7e7e7e;
		}
		.dateSpan{
			width:220rpx;text-align: center;
			margin-bottom: 0;
		}
		.zhi{
			margin: 0 20rpx;
			font-size: 26rpx;
			color: #7e7e7e;
		}
		.active {
			background-color: #de4849;
			color: #fff;
			border-color: #de4849;
		}
	}
}
</style>
